package starComponent.parallelcoordinates;

import java.awt.Color;
import java.awt.Rectangle;

import starComponent.E_PossibleBranches;


/**
 * @author Charles Perin
 * 11 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class A_ParallelBranch implements Comparable<A_ParallelBranch> {
	
	private int position;

	private static final long serialVersionUID = -6312879105601954483L;
	
	public static final int BOUNDING_STEP = 2, BOUNDING_SIDE = 100, BOUNDING_MARGIN=10;

	private ParallelCoordinatesComponent parallelCoordinates;

	private double min, max;

	private String name;

	private double length;

	private Color branchColor;

	protected ParallelLadder ladder;

	private boolean isSnapToTicks = false;
	
	protected Rectangle boundigBox;


	protected E_PossibleBranches branchType;
	
	private int step;
	
	private int x, yStart, yEnd;


	public A_ParallelBranch(ParallelCoordinatesComponent star, E_PossibleBranches branch, int position, Color color, ParallelLadder ladder){
		this.ladder = ladder;
		this.branchType = branch;
		this.branchColor = color;
		this.parallelCoordinates = star;
		this.name = branch.getName();
		this.min = branch.getMinValue();
		this.max = branch.getMaxValue();
		this.position = position;
		this.setSnapToTicks(branch.isSnapToTicks());
	}

	public void updateBranch(){
		int parallelWidth = parallelCoordinates.getWidth();
		int parallelHeight = parallelCoordinates.getHeight();
		int nbBranch = parallelCoordinates.getControler().getNbBranch();
		
		step = (parallelWidth-parallelCoordinates.getControler().getLeftMargin())/nbBranch;
		
		x = step*position + parallelCoordinates.getControler().getLeftMargin();
		yStart = parallelHeight-parallelCoordinates.getControler().getBottomMargin();
		yEnd = parallelCoordinates.getControler().getTopMargin();

		length = Math.abs(yStart-yEnd);

		ladder.updateLadderMap(this);
		buildBoundingBox();

		if(this instanceof ParallelBranchSlider){
			ParallelBranchSlider slider = (ParallelBranchSlider)this;
			slider.getThumb().initPosition();
		}
		else if(this instanceof ParallelBranchBiSlider){
			ParallelBranchBiSlider biSlider = (ParallelBranchBiSlider)this;
			biSlider.getThumbStart().initPosition();
			biSlider.getThumbEnd().initPosition();
		}
	}

	public boolean pointIsInBranch(int x, int y){
		return boundigBox.contains(x, y);
	}

	private void buildBoundingBox(){
		boundigBox = new Rectangle((int)(x-BOUNDING_SIDE/2), (int)(yEnd-BOUNDING_SIDE/2), BOUNDING_SIDE, (int)(Math.abs(yEnd-yStart)+BOUNDING_SIDE));
	}


	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}

	public void setMin(double min) {
		this.min = min;
	}

	public double getMin() {
		return min;
	}

	public void setMax(double max) {
		this.max = max;
	}

	public double getMax() {
		return max;
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = (int) x;
	}

	public double getyStart() {
		return yStart;
	}

	public void setyStart(double yStart) {
		this.yStart = (int) yStart;
	}

	public double getyEnd() {
		return yEnd;
	}

	public void setyEnd(double yEnd) {
		this.yEnd = (int) yEnd;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setBranchColor(Color branchColor) {
		this.branchColor = branchColor;
	}

	public Color getBranchColor() {
		return branchColor;
	}

	public void setLadder(ParallelLadder ladder) {
		this.ladder = ladder;
	}

	public ParallelLadder getLadder() {
		return ladder;
	}

	public ParallelCoordinatesComponent getParallelCoordinates() {
		return parallelCoordinates;
	}

	public void setParallelCoordinates(ParallelCoordinatesComponent parallelCoordinates) {
		this.parallelCoordinates = parallelCoordinates;
	}

	public void setSnapToTicks(boolean isSnapToTicks) {
		this.isSnapToTicks = isSnapToTicks;
	}

	public boolean isSnapToTicks() {
		return isSnapToTicks;
	}

	@Override
	public int compareTo(A_ParallelBranch b) {
		Double a1 = new Double(this.position);
		Double a2 = new Double(b.getPosition());
		if (a1.compareTo(a2) == 1) {
			return -1;
		} else if (a1.compareTo(a2) == -1) {
			return 1;        	
		} else {
			return 0;
		}
	}

	public E_PossibleBranches getBranchType() {
		return branchType;
	}

	public void setBranchType(E_PossibleBranches branchType) {
		this.branchType = branchType;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
	
	public Rectangle getBoundigBox() {
		return boundigBox;
	}

	public void setBoundigBox(Rectangle boundigBox) {
		this.boundigBox = boundigBox;
	}


}
